//
//  BGSellerOrderBasicController.m
//  bigMall
//
//  Created by ios on 16/9/6.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGSellerOrderBasicController.h"
#import "BGDateResult.h"
#import "BGShellerOrderListModel.h"
#import "BGSellerOrderCell.h"
#import "BGDDBasicController.h"
#import "BGSellerListDetailController.h"
@interface BGSellerOrderBasicController () <UITableViewDelegate,UITableViewDataSource> {
    
    UITableView *_tableView;
    int index;
    NSMutableArray *_dataArr;
    UIImageView *imageView;
    UILabel *lable;
    NSMutableArray *strArray;
    
}

@end

@implementation BGSellerOrderBasicController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"卖家订单";
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    
    [self createUI];
    [self initDat];
    
}
- (void)initDat {
    
    _dataArr = [NSMutableArray array];
    
}
- (void)createUI {
    
    _tableView =[[UITableView alloc] initWithFrame:CGRectMake(0, 0, BGWidth, BGHight - 80) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.showsVerticalScrollIndicator = NO;
    
    [self.view addSubview:_tableView];
    
    //刷新的加载
    _tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(index)];
    
    [_tableView.header beginRefreshing];
    imageView = [[UIImageView alloc] initWithFrame:CGRectMake((BGWidth - 70 ) / 2 + 10, (_tableView.height - 109 - 160) / 3, 70, 90)];
    imageView.image = [UIImage imageNamed:@"抱歉~暂无数据"];
    
    lable = [[UILabel alloc] initWithFrame:CGRectMake((BGWidth - 120 ) / 2 + 10, imageView.bottom + 21, 120, 16)];
    lable.text = @"抱歉~暂无数据";
    lable.font = [UIFont systemFontOfSize:16];
    [_tableView addSubview:lable];
    
    [_tableView addSubview:imageView];
    lable.hidden = YES;
    imageView.hidden = YES;
    
}
- (void)addIndex {
    
    index++;
    //    [_dataArr removeAllObjects];
    [self loadInter];
    
}
- (void)index {
    
    index = 1;
    [_dataArr removeAllObjects];
    [self loadInter];
    
}

- (void)loadInter {
    
    [BGDateResult sellerOrderListWithThisPage:[NSString stringWithFormat:@"%d",index] pageNum:@"5" type:self.type success:^(NSObject *result) {
        
        BGShellerOrderListModel * model = [[BGShellerOrderListModel alloc] init];
        model = (BGShellerOrderListModel *)result;
                NSArray *listArr = [NSArray arrayWithArray:model.list];
                //        NSMutableArray *arr = [NSMutableArray array];
                for (BGShellerOrderListListAddayModel *model in listArr) {
                    [_dataArr addObject:model];
                }
                //                       _dataArr = arr;
        if (_dataArr.count == 0 && index == 1) {
            
            [_tableView.header endRefreshing];
            [_tableView.footer endRefreshingWithNoMoreData];
            
           //判断数据源有无
            
            lable.hidden = NO;
            imageView.hidden = NO;
            
            
        }
        
        if ([model.allPage intValue] > index) {
            
            _tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(addIndex)];
            
        }
        
                if (listArr.count == 0) {
                    [_tableView.footer endRefreshingWithNoMoreData];
                    return;
                }
        lable.hidden = YES;
        imageView.hidden = YES;
        
                [_tableView.footer endRefreshing];
                [_tableView.header endRefreshing];
                [_tableView reloadData];
                

    } failure:^(NSError *error) {
        
    }];
    
}

#pragma mark - delegate -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 197;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView registerClass:[BGSellerOrderCell class] forCellReuseIdentifier:@"celId"];
    BGSellerOrderCell *cel = [tableView dequeueReusableCellWithIdentifier:@"celId" forIndexPath:indexPath];
    
    BGShellerOrderListListAddayModel *model = [[BGShellerOrderListListAddayModel alloc] init];
    if (_dataArr != 0) {
        
        model = _dataArr[indexPath.row];
        cel.orderStr = model.idStr;
        cel.leibieStr = model.title;
        cel.timeStr = model.add_time;
        cel.priceStr = model.price;
        cel.numberStr = model.number;
        cel.placeStr = model.delivery_area;
        cel.jiaohuoStr = model.delivery_type_str;
        cel.totalpriceStr = model.total_price;
        
        if ([model.exception_status isEqualToString:@"1"]) {
            cel.typeStr = @"订单已取消";
        } else if ([model.exception_status isEqualToString:@"2"]) {
            cel.typeStr = @"订单已过期";
        } else if ([model.exception_status isEqualToString:@"3"]) {
            cel.typeStr = @"订单冻结中";
        } else if ([model.exception_status isEqualToString:@"4"]) {
            
            if ([model.is_end isEqualToString:@"0"]) {
                cel.typeStr = @"订单仲裁中";
            } else {
                cel.typeStr = @"订单仲裁中";
            }
            
        } else {
            
            switch ([model.status integerValue]) {
                case 0:
                    cel.typeStr = @"等待确认订单";
                    break;
                case 1:
                    
                    if ([model.pay_type isEqualToString:@"onLinePay"] && [model.pay_status isEqualToString:@"1"]) {
                        cel.typeStr = @"确定买家首款";
                    } else {
                        cel.typeStr = @"等待买家付首款";
                    }
                    
                    break;
                case 2:
                    cel.typeStr = @"进行备货";
                    break;
                case 3:
                    
                    if ([model.pay_type isEqualToString:@"onLinePay"] && [model.pay_status isEqualToString:@"1"]) {
                        cel.typeStr = @"确定买家中期款";
                    } else {
                        cel.typeStr = @"等待买家中期款";
                    }
                    
                    break;
                case 4:
                    cel.typeStr = @"提交验收报告";
                    break;
                case 5:
                    cel.typeStr = @"等待买家验收发票";
                    break;
                case 6:
                    
                    if ([model.pay_type isEqualToString:@"onLinePay"] && [model.pay_status isEqualToString:@"3"]) {
                        cel.typeStr = @"确定买家尾款";
                    } else {
                        cel.typeStr = @"等待买家付尾款";
                    }
                    
                    break;
                case 7:
                    
                    if ([model.is_comment isEqualToString:@"1"]) {
                        cel.typeStr = @"等待买家评价";
                    } else {
                        cel.typeStr = @"已结束";
                    }
                
                    break;
                default:
                    break;
            }
            
        }
        
    }
    if (!strArray) {
        strArray = [NSMutableArray array];
    }
    [strArray addObject:cel.typeStr];
    return cel;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    BGShellerOrderListListAddayModel *model = _dataArr[indexPath.row];
    
    BGSellerListDetailController *vc = [[BGSellerListDetailController alloc]init];
    
    vc.strs = strArray[indexPath.row];
    vc.orderId = model.idStr;
    
//    BGDDBasicController *a = [[BGDDBasicController alloc] init];
    
    [self.navigationController pushViewController:vc animated:YES];
    
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
//编辑的样式
//- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
//    
//    // 添加一个删除按钮
//    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
//        NSLog(@"点击了删除");
//        
//        // 1. 更新数据
////        BGOwnBidLogValueModel *model = [[BGOwnBidLogValueModel alloc] init];
////        if (_dataArr != 0) {
////            
////            model = _dataArr[indexPath.row];
////            
////        }
////        
////        [BGDateResult cancelBidWithIdstr:model.idStr success:^(NSObject *result) {
////            
////            BGCancelBidModel *a = [[BGCancelBidModel alloc] init];
////            a = (BGCancelBidModel *)result;
////            if ([a.status isEqualToString:@"1"]) {
////                [self showMessage:a.msg];
////            }else {
////                [self showMessage:a.msg];
////            }
////            
////        } failure:^(NSError *error) {
////            
////        }];
//        
//        // 1. 更新数据
//        NSArray *indexPaths = @[indexPath]; // 构建 索引处的行数 的数组
//        
//        [_dataArr removeObjectAtIndex:indexPath.row];
//        
//        [tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:(UITableViewRowAnimationLeft)];
//        // 2. 更新UI
//        //        [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)];
//    }];
//    
//    // 删除一个置顶按钮
//    UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
//        NSLog(@"点击了置顶");
//        
//        
//        // 2. 更新UI
//        NSIndexPath *firstIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
//        [tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];
//    }];
//    topRowAction.backgroundColor = [UIColor blueColor];
//    
//    // 添加一个更多按钮
//    UITableViewRowAction *moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"更多"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
//        NSLog(@"点击了更多");
//        
//        [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
//    }];
//    moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
//    
//    // 将设置好的按钮放到数组中返回
//    return @[deleteRowAction];
//    
//}
- (void)showMessage:(NSString *)message {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    
    [alertController addAction:okAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
